package top.mhtools.pigeon.common.factory.ding;

import top.mhtools.pigeon.common.enums.DingMessageTypeEnum;
import top.mhtools.pigeon.common.enums.SendTypeEnum;
import top.mhtools.pigeon.common.factory.Message;
import top.mhtools.pigeon.common.persistence.SendMessageReqDTO;
import top.mhtools.pigeon.common.persistence.SendMessageRequest;
import top.mhtools.pigeon.common.persistence.SimpleSendMessageReqDTO;
import top.mhtools.pigeon.ding.dto.DingMessageReqDTO;

import java.util.Optional;

/**
 * 钉钉消息产品类
 *
 * @author machen
 * @date 2020/5/27 20:36
 */
public class DingMessage implements Message {

    @Override
    public SendMessageRequest buildMessage(SimpleSendMessageReqDTO simpleMessage) {
        SendMessageReqDTO sendMessageReqDTO = new SendMessageReqDTO();
        DingMessageReqDTO dingMessageReqDTO = new DingMessageReqDTO();

        // 钉钉入参赋值
        dingMessageReqDTO.setToken(simpleMessage.getToken());
        dingMessageReqDTO.setMsgtype(DingMessageTypeEnum.MD.getName());
        dingMessageReqDTO.setTemplateId(
                Optional.ofNullable(simpleMessage.getTemplateId()).orElse(""));
        dingMessageReqDTO.setAtMobiles(simpleMessage.getSendTo());

        // 钉钉MD入参赋值
        DingMessageReqDTO.Markdown markdown = new DingMessageReqDTO.Markdown();
        markdown.setText(simpleMessage.getContent());
        markdown.setTextMap(simpleMessage.getTextMap());

        dingMessageReqDTO.setMarkdown(markdown);

        // 全局入参赋值
        sendMessageReqDTO.setSendType(SendTypeEnum.DING_TALK.getName());
        sendMessageReqDTO.setDingMessageReqDTO(dingMessageReqDTO);

        return sendMessageReqDTO;
    }
}
